package org.xingole.cookbook;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.xingole.cookbook.helper.CookbookHelper;

/**
 * Simple snippet which shows how to open an existing repository.
 * 
 * @author xingole
 */
public class OpenRepository {
    public static void main(String[] args) throws IOException, NoFilepatternException, GitAPIException {
        // first create a test-repository, the return is including the .git directory here!
        File repoDir = createSampleGitReop();

        // now open the resulting repository with a FileRepositoryBuilder
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        try (Repository repository = builder.setGitDir(repoDir)
                                            .readEnvironment()// scan environment GIT_* variables
                                            .findGitDir()     // scan up the file system tree
                                            .build()) 
        {
            System.out.println("Having repository: " + repository.getDirectory());
            
            // the Ref holds an ObjectId for any type of object (tree, commit, blob, tag)
            Ref head = repository.exactRef("refs/heads/master");
            System.out.println("Ref of refs/heads/master: " + head);
        }

        // clean up here to not keep using more and more disk-space for these samples
        FileUtils.deleteDirectory(repoDir.getParentFile());
    }

    private static File createSampleGitReop() throws IOException, NoFilepatternException, GitAPIException {
        try (Repository repository = CookbookHelper.createNewRepository()) {
            System.out.println("Temporary repository at " + repository.getDirectory());

            // create the file
            File myFile = new File(repository.getDirectory().getParent(), "testfile");
            if(!myFile.createNewFile()) {
                throw new IOException("Could not create file " + myFile);
            }

            try(Git git = new Git(repository)) {
                // run the add-call
                git.add()                       // 1. start the git-add operation 
                    .addFilepattern("testfile") // 2. choose the added file format pattern 
                    .call();                    // 3. executes the add operation

                // and then commit the changes
                git.commit()                     // 1. start the git-commit operation
                    .setMessage("Added testfile")// 2. add commit messages
                    .call();                     // 3. execute the commit operation
            }

            System.out.println("Added file " + myFile + "to repository at" + repository.getDirectory());

            return repository.getDirectory();
        }
    }
}
